// index.js
// 获取应用实例
const app = getApp()
const api = app.globalData.api

Page({
    data: {
        info: [],
    },
    async getUserProfile() {
        let res = await api.curlGet('?cla=myApp&fun=home')
        if (res.code === 1) {
            this.setData({
                info: res.data.result
            })
        } else {
            api.showToast(res.warn)
        }
    }, // 拨打电话
    call(obj) {
        const tel = obj.currentTarget.dataset.tel
        wx.makePhoneCall({
            phoneNumber: tel
        })
    }, // 登录
    async login() {
        wx.setStorageSync('token', "-1")
        wx.request({
            url: app.globalData.root + '?cla=loginApp&fun=login', method: 'POST',
            header: {
                "Content-Type": "application/x-www-form-urlencoded",
                "Auth": "-1",
                'Source': 'erp3'
            },
            data: {},
            success(resp) {
                const res = resp.data
                if (res.code === 1) {
                    wx.setStorageSync('token', res.data.token)
                }
            }, fail(err) {
                console.log(err)
            }
        })
    },
    onLoad: async function (options) {
        wx.getSystemInfo({
            success (res) {
                console.log(res.enableDebug)
            }
        })
        const id = options ? options.id : ''
        if (id === 'admin') {
            if (wx.getStorageSync('token') === '-1') {
                wx.clearStorageSync('token')
            }
            await this.login()
        }
        await this.getUserProfile()
    },

    /// 长按清理信息
    clearStorage: function (act) {
        app.clearStorage()
    },
    /// 长按打开debug
    openDebug: function (act) {
        wx.showModal({
            title: '提示',
            content: `是否打开debug模式`,
            success: function (res) {
                //判断用户是否点击了确定
                if (res.confirm) {
                    wx.setEnableDebug({
                        enableDebug: true
                    })
                    // 重新登录
                    app.getUid()
                }
            }
        })
    },
})
